using System;
using System.Collections.Generic;
using System.Text;

namespace Deepcode.Flux.Core.Systems.Audit
{
	/// <summary>
	/// SystemEventConfiguration describes the level of 
	/// logging that will take place on the installation. The
	/// information is held in the FluxRegistry
	/// </summary>
	public class SystemEventConfiguration
	{
		private static bool Loaded = false;
		private static bool _LogTrace = false;
		private static bool _LogInformation = false;
		private static bool _LogImportant = false;
		private static bool _LogCritical = true;
		private static bool _LogError = true;

		/// <summary>
		/// Set/Get whether or not the system will log trace information - default false
		/// A trace level event is debugging information and is generally only used on a
		/// development installation, but turned off in production environments. These messages
		/// are generally only useful to the developer.
		/// </summary>
		public static bool LogTrace
		{
			get
			{
				if (!Loaded) Load();
				return _LogTrace;
			}
			set
			{
				if (!Loaded) Load();
				_LogTrace = value;
				Save();
			}
		}
		/// <summary>
		/// Whether or not the system will log information events - default true
		/// An information event is used to log useful information such as a script 
		/// starting or someone logging into the system. These messages are deemed 
		/// useful to the administrator of the system for tracing and tracking only.
		/// </summary>
		public static bool LogInformation
		{
			get
			{
				if (!Loaded) Load();
				return _LogInformation;
			}
			set
			{
				if (!Loaded) Load();
				_LogInformation = value;
				Save();
			}
		}
		/// <summary>
		/// Whether or not the system will log important events - default true
		/// An Important event is activity within the system that an administrator
		/// might need to know about. For example, User X deleted user Y, User X 
		/// deleted the content object titled "ABCDEFG". Audit level events.
		/// </summary>
		public static bool LogImportant
		{
			get
			{
				if (!Loaded) Load();
				return _LogImportant;
			}
			set
			{
				if (!Loaded) Load();
				_LogImportant = value;
				Save();
			}
		}
		/// <summary>
		/// Whether or not the system will log critical events - default true
		/// A Critical event may need administrative notification or action. For example
		/// a user attempting unauthorised access to a module, or user X installed 
		/// the YZ application etc.
		/// </summary>
		public static bool LogCritical
		{
			get
			{
				if (!Loaded) Load();
				return _LogCritical;
			}
			set
			{
				if (!Loaded) Load();
				_LogCritical = value;
				Save();
			}
		}
		/// <summary>
		/// Whether or not the system will log error events - default true
		/// An error event is a fatal module/task or system error that would 
		/// generally be populated using exception handling in a module, and would
		/// normally be reflective of erroneous data or bugs in a solution.
		/// </summary>
		public static bool LogError
		{
			get
			{
				if (!Loaded) Load();
				return _LogError;
			}
			set
			{
				if (!Loaded) Load();
				_LogError = value;
				Save();
			}
		}

		private static void Load()
		{
			try
			{
				Deepcode.Flux.Core.Systems.Registry.FluxRegistry r =
					new Deepcode.Flux.Core.Systems.Registry.FluxRegistry(
						Deepcode.Flux.Core.Systems.Registry.FluxRegistryRoot.ABSOLUTEROOT,
						"/Events/Logging");

				_LogTrace = r.GetKey("LogTrace", false);
				_LogInformation = r.GetKey("LogInformation", true);
				_LogImportant = r.GetKey("LogImportant", true);
				_LogCritical = r.GetKey("LogCritical", true);
				_LogError = r.GetKey("LogError", true);
			}
			catch { }
			// Try/Catch block in place for first install of Flux where registry doesn't exist!
			Loaded = true;
		}

		private static void Save()
		{
			Deepcode.Flux.Core.Systems.Registry.FluxRegistry r =
				new Deepcode.Flux.Core.Systems.Registry.FluxRegistry(
					Deepcode.Flux.Core.Systems.Registry.FluxRegistryRoot.ABSOLUTEROOT,
					"/Events/Logging");

			r.SetKey("LogTrace", _LogTrace);
			r.SetKey("LogInformation", _LogInformation);
			r.SetKey("LogImportant", _LogImportant);
			r.SetKey("LogCritical", _LogCritical);
			r.SetKey("LogError", _LogError);
		}
	}
}
